<template>
  <Header :title="adressEdit"></Header>
  <van-address-edit
    :area-list="areaList"
    show-delete
    :address-info="addressInfo"
    show-set-default
    show-search-result
    :area-columns-placeholder="['请选择', '请选择', '请选择']"
    @save="onSave"
    @delete="onDelete"
  />
</template>

<script setup>
import { computed, onMounted, ref } from "vue";
import { useCartStore } from "../../store/CartSore";
import Header from "../home/components/Header.vue";
import { useRoute, useRouter } from "vue-router";
const router=useRouter()
const areaList = ref({
  province_list: {
    110000: "广东省",
    120000: "浙江省",
  },
  city_list: {
    110100: "广州市",
    110200: "深圳市",
    120100: "杭州市",
    120200: "宁波市",
  },
  county_list: {
    110101: "天河区",
    110102: "海珠区",
    120102: "上城区",
    130102: "下城区",
  },
});
const addressInfo = ref({});
const onSave = (content) => {
  console.log(content);
  if(route.query.type==="add"){
    carStore.Addaddress(content)
  }
  else{
    carStore.editAddress(content)
  }
  showToast('保存成功');
  setTimeout(()=>{
    router.back()
  },1000)
};
const onDelete = (content) => {
  carStore.deleteAddress(content)
  showToast('删除成功');
  setTimeout(()=>{
    router.back()
  },1000)

};
const carStore = useCartStore();
const route = useRoute();
const adressEdit = computed(() => {
  return route.query.type === "add" ? "地址新增" : "地址编辑";
});
const init = () => {
  carStore.userAddress.forEach((item) => {
    if (item.id === Number(route.query.id)) {
      addressInfo.value = item;
    }
  });
};
onMounted(() => {
  init();
});
</script>

<style scoped lang="less">
/deep/ .van-button--primary {
  background-color: #ffc400;
  border-color: #ffc400;
}
/deep/ .van-switch--on {
  background-color: #ffc400;
}
</style>
